<?php

class ApiController extends BaseController {
	public function getIndex() {
		echo "welcome to PLNRBBDist3 api";
	}
    
    public function getInputpenyulang() {
		$returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Input data to table penyulang success.";
        $isError          = FALSE;
        $missingParams    = null;
        
        $input                  = Input::all();
        $tanggal                = (isset($input['tanggal']))                ? $input['tanggal']                         : null;
        $area                   = (isset($input['area']))                   ? $input['area']                            :null;
        $rayon                  = (isset($input['rayon']))                  ? $input['rayon']                           :null;
        $feeder                 = (isset($input['feeder']))                 ? $input['feeder']                          : null;
        $nama_gardu             = (isset($input['nama_gardu']))             ? $input['nama_gardu']                      : null;
        $merk_trafo             = (isset($input['merk_trafo']))             ? $input['merk_trafo']                      : null;
        $lokasi                 = (isset($input['lokasi']))                 ? $input['lokasi']                          : null;
        $kapasitas              = (isset($input['kapasitas']))              ? intval($input['kapasitas'])               : null;
        $arus_R_kiri            = (isset($input['arus_R_kiri']))            ? intval($input['arus_R_kiri'])             : null;
        $arus_R_kanan           = (isset($input['arus_R_kanan']))           ? intval($input['arus_R_kanan'])            : null;
        $arus_S_kiri            = (isset($input['arus_S_kiri']))            ? intval($input['arus_S_kiri'])             : null;
        $arus_S_kanan           = (isset($input['arus_S_kanan']))           ? intval($input['arus_S_kanan'])            : null;
        $arus_T_kiri            = (isset($input['arus_T_kiri']))            ? intval($input['arus_T_kiri'])             : null;
        $arus_T_kanan           = (isset($input['arus_T_kanan']))           ? intval($input['arus_T_kanan'])            : null;
        $arus_N_kiri            = (isset($input['arus_N_kiri']))            ? intval($input['arus_N_kiri'])             : null;
        $arus_N_kanan           = (isset($input['arus_N_kanan']))           ? intval($input['arus_N_kanan'])            : null;
        $tegangan_FN            = (isset($input['tegangan_FN']))            ? intval($input['tegangan_FN'])             : null;
        $tegangan_FF            = (isset($input['tegangan_FF']))            ? intval($input['tegangan_FF'])             : null;
        $tegangan_ujung_kanan   = (isset($input['tegangan_ujung_kanan']))   ? intval($input['tegangan_ujung_kanan'])    : null;
        $tegangan_ujung_kiri    = (isset($input['tegangan_ujung_kiri']))    ? intval($input['tegangan_ujung_kiri'])     : null;
        
        if(!isset($area)){
            $missingParams[] = "area";
        }
        if(!isset($rayon)){
            $missingParams[] = "rayon";
        }
        if(!isset($feeder)){
            $missingParams[] = "feeder";
        }
        if(!isset($tanggal)){
            $missingParams[] = "tanggal";
        }
        if(!isset($nama_gardu)){
            $missingParams[] = "nama_gardu";
        }
        if(!isset($merk_trafo)){
            $missingParams[] = "merk_trafo";
        }
        if(!isset($lokasi)){
            $missingParams[] = "lokasi";
        }
        if(!isset($kapasitas)){
            $missingParams[] = "kapasitas";
        }
        if(!isset($arus_R_kiri)){
            $missingParams[] = "arus_R_kiri";
        }
        if(!isset($arus_R_kanan)){
            $missingParams[] = "arus_R_kanan";
        }
        if(!isset($arus_S_kiri)){
            $missingParams[] = "arus_S_kiri";
        }
        if(!isset($arus_S_kanan)){
            $missingParams[] = "arus_S_kanan";
        }
        if(!isset($arus_T_kiri)){
            $missingParams[] = "arus_T_kiri";
        }
        if(!isset($arus_T_kanan)){
            $missingParams[] = "arus_T_kanan";
        }
        if(!isset($arus_N_kiri)){
            $missingParams[] = "arus_N_kiri";
        }
        if(!isset($arus_N_kanan)){
            $missingParams[] = "arus_N_kanan";
        }
        if(!isset($tegangan_FN)){
            $missingParams[] = "tegangan_FN";
        }
        if(!isset($tegangan_FF)){
            $missingParams[] = "tegangan_FF";
        }
        if(!isset($tegangan_ujung_kanan)){
            $missingParams[] = "tegangan_ujung_kanan";
        }
        if(!isset($tegangan_ujung_kiri)){
            $missingParams[] = "tegangan_ujung_kiri";
        }
        
        
        if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
        
        if(!$isError){
            try {
                $arusnominal                    = floatval(number_format((float)(($kapasitas * 1000) / (400 * sqrt(3))), 2, '.', ''));
                $arus_R_sumber                  = $arus_R_kiri + $arus_R_kanan;
                $arus_S_sumber                  = $arus_S_kiri + $arus_S_kanan;
                $arus_T_sumber                  = $arus_T_kiri + $arus_T_kanan;
                $arus_N_sumber                  = $arus_N_kiri + $arus_N_kanan;
                
                $some                           = array($arus_R_sumber, $arus_S_sumber, $arus_T_sumber);
                $some_kiri                      = array($arus_R_kiri,   $arus_S_kiri,   $arus_T_kiri);
                $some_kanan                     = array($arus_R_kanan,  $arus_S_kanan,  $arus_T_kanan);
                
                $beban_total_kVA                = floatval(number_format((float)((($arus_R_sumber + $arus_S_sumber + $arus_T_sumber) * $tegangan_FN) / 1000), 2, '.', ''));
                $beban_total_percent            = floatval(number_format((float)(($beban_total_kVA / $kapasitas) * 100), 2, '.', ''));
                $beban_fasa_tertinggi_percent   = floatval(number_format((float)((max($some) / $arusnominal) * 100), 2, '.', ''));
                $beban_fasa_tertinggi_kVA       = floatval(number_format((float)(($beban_fasa_tertinggi_percent * $kapasitas) / 100), 2, '.', ''));
                $kesetimbangan                  = floatval(number_format((float)(((max($some) - min($some)) / (array_sum($some) / 3)) * 100), 2, '.', ''));
                $kesetimbangan_kiri             = floatval(number_format((float)(((max($some_kiri) - min($some_kiri)) / (array_sum($some_kiri) / 3)) * 100), 2, '.', ''));
                $kesetimbangan_kanan            = floatval(number_format((float)(((max($some_kanan) - min($some_kanan)) / (array_sum($some_kanan) / 3)) * 100), 2, '.', ''));
                $inetral                        = floatval(number_format((float)(($arus_N_sumber / (array_sum($some) / 3)) * 100), 2, '.', ''));
                $inetral_kiri                   = floatval(number_format((float)(($arus_N_kiri / (array_sum($some_kiri) / 3)) * 100), 2, '.', ''));
                $inetral_kanan                  = floatval(number_format((float)(($arus_N_kanan / (array_sum($some_kanan) / 3)) * 100), 2, '.', ''));
                
                $ratarata_sumber = ($arus_R_sumber  + $arus_S_sumber + $arus_T_sumber) / 3;
                $ratarata_kiri   = ($arus_R_kiri    + $arus_S_kiri   + $arus_T_kiri) / 3;
                $ratarata_kanan  = ($arus_R_kanan   + $arus_S_kanan  + $arus_T_kanan) / 3;

                $sumber_R   = $arus_R_sumber / $ratarata_sumber;
                $sumber_S   = $arus_S_sumber / $ratarata_sumber;
                $sumber_T   = $arus_T_sumber / $ratarata_sumber;

                $kiri_R     = $arus_R_kiri / $ratarata_kiri;
                $kiri_S     = $arus_S_kiri / $ratarata_kiri;
                $kiri_T     = $arus_T_kiri / $ratarata_kiri;

                $kanan_R    = $arus_R_kanan / $ratarata_kanan;
                $kanan_S    = $arus_S_kanan / $ratarata_kanan;
                $kanan_T    = $arus_T_kanan / $ratarata_kanan;

                $ketidakseimbangan_sumber = floatval(number_format((float)(((abs($sumber_R - 1) + abs($sumber_S - 1) + abs($sumber_T - 1)) / 3) * 100), 2, '.', '')); 
                $ketidakseimbangan_kiri   = floatval(number_format((float)(((abs($kiri_R - 1) + abs($kiri_S - 1) + abs($kiri_T - 1)) / 3) * 100), 2, '.', '')); 
                $ketidakseimbangan_kanan  = floatval(number_format((float)(((abs($kanan_R - 1) + abs($kanan_S - 1) + abs($kanan_T - 1)) / 3) * 100), 2, '.', ''));

                $penyulang                      = new Penyulang;
                
                $penyulang->tanggal                             = new DateTime($tanggal);
                $penyulang->area                                = $area;
                $penyulang->rayon                               = $rayon;
                $penyulang->feeder                              = $feeder;
                $penyulang->nama_gardu                          = $nama_gardu;
                $penyulang->merk_trafo                          = $merk_trafo;
                $penyulang->lokasi                              = $lokasi;
                $penyulang->datatrafo_kapasitas                 = $kapasitas;
                $penyulang->datatrafo_arusnominal               = $arusnominal;
                $penyulang->datapengukuran_arus_R_sumber        = $arus_R_sumber;
                $penyulang->datapengukuran_arus_R_kiri          = $arus_R_kiri;
                $penyulang->datapengukuran_arus_R_kanan         = $arus_R_kanan;
                $penyulang->datapengukuran_arus_S_sumber        = $arus_S_sumber;
                $penyulang->datapengukuran_arus_S_kiri          = $arus_S_kiri;
                $penyulang->datapengukuran_arus_S_kanan         = $arus_S_kanan;
                $penyulang->datapengukuran_arus_T_sumber        = $arus_T_sumber;
                $penyulang->datapengukuran_arus_T_kiri          = $arus_T_kiri;
                $penyulang->datapengukuran_arus_T_kanan         = $arus_T_kanan;
                $penyulang->datapengukuran_arus_N_sumber        = $arus_N_sumber;
                $penyulang->datapengukuran_arus_N_kiri          = $arus_N_kiri;
                $penyulang->datapengukuran_arus_N_kanan         = $arus_N_kanan;
                $penyulang->datapengukuran_tegangan_FN          = $tegangan_FN;
                $penyulang->datapengukuran_tegangan_FF          = $tegangan_FF;
                $penyulang->datapengukuran_tegangan_ujung_kanan = $tegangan_ujung_kanan;
                $penyulang->datapengukuran_tegangan_ujung_kiri  = $tegangan_ujung_kiri;
                $penyulang->beban_total_kVA                     = $beban_total_kVA;
                $penyulang->beban_total_percent                 = $beban_total_percent;
                $penyulang->beban_fasa_tertinggi_kVA            = $beban_fasa_tertinggi_kVA;
                $penyulang->beban_fasa_tertinggi_percent        = $beban_fasa_tertinggi_percent;
                $penyulang->kesetimbangan                       = $kesetimbangan;
                $penyulang->kesetimbangan_kiri                  = $kesetimbangan_kiri;
                $penyulang->kesetimbangan_kanan                 = $kesetimbangan_kanan;
                $penyulang->inetral                             = $inetral;
                $penyulang->inetral_kiri                        = $inetral_kiri;
                $penyulang->inetral_kanan                       = $inetral_kanan;
                $penyulang->ketidakseimbangan_sumber            = $ketidakseimbangan_sumber;
                $penyulang->ketidakseimbangan_kiri              = $ketidakseimbangan_kiri;
                $penyulang->ketidakseimbangan_kanan             = $ketidakseimbangan_kanan;
                
                $penyulang->save();
                
                $result['penyulang'] = $penyulang;
//                $result['penyulang'] = array(
//                    'rata-rata' => array($ratarata_sumber, $ratarata_kiri, $ratarata_kanan),
//                    'arus-kiri' => array($arus_R_kiri, $arus_S_kiri, $arus_T_kiri)
//                );
                
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
	}
    
    public function getRegister() {
		$returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Register account success.";
        $isError          = FALSE;
        $missingParams    = null;
		
        $input            = Input::all();
        $username         = (isset($input['username'])) ? $input['username']:null;
        $password         = (isset($input['password'])) ? $input['password']:null;
        $role             = (isset($input['role'])) ? $input['role']:null;
		
        if(!isset($username)){
            $missingParams[] = "username";
        }
        if(!isset($password)){
            $missingParams[] = "password";
        }
		if(!isset($role)){
            $missingParams[] = "role";
        }
        
		if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
		
        if(!$isError){ 
            try {
				
				$count		= User::whereRaw("username = '$username'")->count();
				
				if ($count == 0) {
					$account = new User;
					
					$account->username 	= $username;
					$account->password 	= $password;
					$account->role 		= $role;
					
					$account->save();
					
					$result['account'] = array(
                        'username' 	=> $username,
                        'role' 		=> $role
                    );
				} else {
					$response 	= "FAILED";
					$statusCode = 200;
					$message 	= "Someone already has that username.";
				}
				
				
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
	}	
	
	public function getLogin() {
		$returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Login success.";
        $isError          = FALSE;
        $missingParams    = null;
		
        $input            = Input::all();
        $username         = (isset($input['username'])) ? $input['username']:null;
        $password         = (isset($input['password'])) ? $input['password']:null;
		
        if(!isset($username)){
            $missingParams[] = "username";
        }
        if(!isset($password)){
            $missingParams[] = "password";
        }
        
		if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
		
        if(!$isError){ 
            try {
				$count		= User::whereRaw("username = '$username'")->count();
				
				if ($count == 0) {
					$response 	= "FAILED";
					$statusCode = 200;
					$message 	= "Username not found.";
				} else {
					$account 	= User::whereRaw("username = '$username'")->first();
					
					if ($account->password == $password) {
						$result['account'] = array(
                            'username' 	=> $username,
                            'role' 		=> $account->role
                        );
					} else {
						$response 	= "FAILED";
						$statusCode = 200;
						$message 	= "Username and password did not match.";
					}
				}
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
	}
    
    public function getDeleteuser() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "";
        $isError          = FALSE;
        $missingParams    = null;

        $input            = Input::all();
        $username         = (isset($input['username'])) ? $input['username']:null;
        
        if(!isset($username)){
            $missingParams[] = "username";
        }
        
        if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
		
        if(!$isError){
            try {
                $count		= User::whereRaw("username = '$username'")->count();
				
				if ($count == 0) {
					$response 	= "FAILED";
					$statusCode = 200;
					$message 	= "Username not found.";
				} else {
					$account 	= User::find($username);
					$account->delete();
                    
                    $message    = "Account $username has been deleted.";
				}
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getUpdateuser() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "";
        $isError          = FALSE;
        $missingParams    = null;
        $updatedParams    = null;
		
        $input            = Input::all();
        $username         = (isset($input['username'])) ? $input['username']:null;
        $password         = (isset($input['password'])) ? $input['password']:null;
        $role             = (isset($input['role'])) ? $input['role']:null;
		
        if(!isset($username)){
            $missingParams[] = "username";
        }
        
		if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
		
        if(!$isError){ 
            try {
				$count		= User::whereRaw("username = '$username'")->count();
				
				if ($count == 0) {
					$response 	= "FAILED";
					$statusCode = 200;
					$message 	= "Username not found.";
				} else {
					$account 	= User::find($username);
                    
                    if(isset($password)){
                        $account->password 	= $password;
                        $updatedParams[]    = "password;";
                    }
                    if(isset($role)){
                        $account->role 		= $role;
                        $updatedParams[]    = "role";
                    }
                    
                    $account->save();
                    
                    $message    = "Account $username has been updated. Updated data : {".implode(', ', $updatedParams)."}";
				}
				
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getPenyulang() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Get all penyulang data success.";
        $isError          = FALSE;
        $missingParams    = null;

        $input = Input::all();

        if(!$isError){
            try {
                $result['penyulang'] = Penyulang::all();
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getUsers() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Get all users data success.";
        $isError          = FALSE;
        $missingParams    = null;

        $input = Input::all();

        if(!$isError){
            try {
                $result['users'] = User::all(array('username', 'role'));
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getGangguan() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Get all gangguan data success.";
        $isError          = FALSE;
        $missingParams    = null;

        $input = Input::all();

        if(!$isError){
            try {
                $result['gangguan'] = Gangguan::all();
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getKomponen() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Get all komponen data success.";
        $isError          = FALSE;
        $missingParams    = null;

        $input = Input::all();

        if(!$isError){
            try {
                $result['komponen'] = Komponen::all();
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getTindakan() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Get all tindakan data success.";
        $isError          = FALSE;
        $missingParams    = null;

        $input = Input::all();

        if(!$isError){
            try {
                $result['tindakan'] = Tindakan::all();
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function postInputgangguan() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Input data to table gangguan success.";
        $isError          = FALSE;
        $missingParams    = null;
		
        $input            = Input::all();
        $tanggal          = (isset($input['tanggal']))      ? $input['tanggal']             :null;
        $lokasi           = (isset($input['lokasi']))       ? $input['lokasi']              :null;
        $area             = (isset($input['area']))         ? $input['area']                :null;
        $rayon            = (isset($input['rayon']))        ? $input['rayon']               :null;
        $feeder           = (isset($input['feeder']))       ? $input['feeder']              :null;
        $longitude        = (isset($input['longitude']))    ? floatval($input['longitude']) :null;
        $latitude         = (isset($input['latitude']))     ? floatval($input['latitude'])  :null;
        $nama_tiang       = (isset($input['nama_tiang']))   ? $input['nama_tiang']          :null;
        $komponen         = (isset($input['komponen']))     ? $input['komponen']            :null;
        $tindakan         = (isset($input['tindakan']))     ? $input['tindakan']            :null;
        $keterangan       = (isset($input['keterangan']))   ? $input['keteragan']           :null;
        $foto             = (Input::hasFile('foto'))        ? Input::file('foto')           :null;        
        
        if(!isset($tanggal)){
            $missingParams[] = "tanggal";
        }
        if(!isset($area)){
            $missingParams[] = "area";
        }
        if(!isset($rayon)){
            $missingParams[] = "rayon";
        }
        if(!isset($feeder)){
            $missingParams[] = "feeder";
        }
        if(!isset($longitude)){
            $missingParams[] = "longitude";
        }
        if(!isset($latitude)){
            $missingParams[] = "latitude";
        }
        if(!isset($nama_tiang)){
            $missingParams[] = "nama_tiang";
        }
        if(!isset($komponen)){
            $missingParams[] = "komponen";
        }
        if(!isset($tindakan)){
            $missingParams[] = "tindakan";
        } // */
        if(!isset($foto)){
            $missingParams[] = "foto";
        }
        if(!isset($keterangan)){
            $missingParams[] = "keterangan";
        }
        
		if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
		
        if(!$isError){ 
            try {
                $destinationPath  = public_path().'/files/photos';
                $extension        = $foto->getClientOriginalExtension();
                $filename         = time()."_".str_random(12).".".$extension;
                
                $foto->move($destinationPath, $filename);
                
                $gangguan = new Gangguan;
                
                $gangguan->tanggal      = new DateTime($tanggal);
                $gangguan->area         = $area;
                $gangguan->rayon        = $rayon;
                $gangguan->feeder       = $feeder;
                $gangguan->longitude    = $longitude;
                $gangguan->latitude     = $latitude;
                $gangguan->nama_tiang   = $nama_tiang;
                $gangguan->komponen     = $komponen;
                $gangguan->tindakan     = $tindakan;
                $gangguan->foto         = $filename;
                $gangguan->keterangan   = $keterangan;    
                
                if(isset($lokasi)){
                    $gangguan->lokasi   = $lokasi;
                } 
                
                $gangguan->save();
                
                $result['gangguan'] = $gangguan;
                // */
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getDeletegangguan() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "";
        $isError          = FALSE;
        $missingParams    = null;

        $input            = Input::all();
        $id               = (isset($input['id'])) ? $input['id']:null;
        
        if(!isset($id)){
            $missingParams[] = "id";
        }
        
		if(isset($missingParams)){
            $isError = TRUE;
            $response = "FAILED";
            $statusCode = 400;
            $message = "Missing parameters : {".implode(', ', $missingParams)."}";
        }
		
        if(!$isError){
            try {
                $count		= Gangguan::whereRaw("id = '$id'")->count();
				
				if ($count == 0) {
					$response 	= "FAILED";
					$statusCode = 200;
					$message 	= "Data not found.";
				} else {
					$destinationPath  = public_path().'/files/photos/';
                    
                    $gangguan 	= Gangguan::find($id);
					//$gangguan->delete();
                    $filename   = $gangguan->foto;
                    
                    $realpath   = $destinationPath.$filename;
                    
                    if (is_readable($realpath)) {
                        if (unlink($realpath)) {
                            $gangguan->delete();
                            $message    = "Data gangguan has been deleted.";
                        } else {
                            $response 	= "FAILED";
					        $statusCode = 200;
                            $message    = "Error deleting photo.";
                        }
                    } else {
                        $gangguan->delete();
                        $message    = "Data gangguan has been deleted. But, file not found.";
                    }
                    
				}
            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getLocations() {
        $returnData       = array();
        $response         = "OK";
        $statusCode       = 200;
        $result           = null;
        $message          = "Get all data locations success.";
        $isError          = FALSE;
        $missingParams    = null;
		
        if(!$isError){
            try {
                $returned = array();
                
                $areas = Locations::groupBy('area')->get();
                foreach ($areas as $area) {
                    $returned[$area->area] = array();
                    $rayons = Locations::where('area','=',$area->area)->groupBy('rayon')->get();
                    
                    foreach ($rayons as $rayon) {
                        $returned[$area->area][$rayon->rayon] = array();
                        $penyulangs = Locations::where('rayon','=',$rayon->rayon)->get();
                        
                        foreach($penyulangs as $penyulang){
                            array_push($returned[$area->area][$rayon->rayon], $penyulang->penyulang);
                        }
                    }
                }
                
                $result['locations'] = $returned;

            } catch (Exception $e) {
                $response = "FAILED";
                $statusCode = 400;
                $message = $e->getMessage();
            } // */
        }

        $returnData = array(
            'response' => $response,
            'status_code' => $statusCode,
            'message' => $message,
            'result' => $result
            );

//        print_r($returned);
        return Response::json($returnData, $statusCode)->header('access-control-allow-origin', '*');
    }
    
    public function getCheckcheck() {
        date_default_timezone_set('Asia/Pontianak');
        $date = date('d m Y h:i:s a', time());
        
        echo "Current time : $date";
    }
    
    public function missingMethod($parameters = array()) {
        echo "API not found. \n";
        echo "Available API : register, login, deleteuser, deletegangguan, updateuser, users, penyulang, komponen, tindakan, gangguan, inputpenyulang, inputgangguan, locations";
    }
}

?>